package br.com.sistemasaude.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

public class GenericDAO<T> {	
	
	private final EntityManager em;
	private Class<T> classe;
	
	 public GenericDAO(EntityManager em, Class<T> classe){
		 this.classe = classe;
		 this.em = em;
	 }
	
		
	public void salvar(T obj){
		this.em.persist(obj);
	}

	public T buscar(int id){
		return em.getReference(classe, id);
	}
	
	public void remove(T obj){
		this.em.remove(obj);
	}
	
	@SuppressWarnings("unchecked")
	public List<T> listar(){
		return em.createQuery("from " + classe.getName() ).getResultList();
	}
	
	public void update(T obj) {
		
			this.em.merge(obj);
			
	}
}
